package me.zhengjie.modules.ejweixin.service.impl;

import me.zhengjie.modules.ejweixin.domain.NgConstructionReservation;
import me.zhengjie.modules.ejweixin.domain.NgConstructionSiteInfo;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionSiteInfoMapper;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.ejweixin.service.NgConstructionReservationService;
import me.zhengjie.modules.ejweixin.domain.vo.NgConstructionReservationQueryCriteria;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionReservationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;
import me.zhengjie.websocket.MsgType;
import me.zhengjie.websocket.SocketMsg;
import me.zhengjie.websocket.WebSocketServer;

/**
* @description 服务实现
* @author cy
* @date 2025-06-17
**/
@Service
@RequiredArgsConstructor
public class NgConstructionReservationServiceImpl extends ServiceImpl<NgConstructionReservationMapper, NgConstructionReservation> implements NgConstructionReservationService {

    private final NgConstructionReservationMapper ngConstructionReservationMapper;

    private final NgConstructionSiteInfoMapper ngConstructionSiteInfoMapper;

    @Override
    public PageResult<NgConstructionReservation> queryAll(NgConstructionReservationQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(ngConstructionReservationMapper.findAll(criteria, page));
    }

    @Override
    public List<NgConstructionReservation> queryAll(NgConstructionReservationQueryCriteria criteria){
        return ngConstructionReservationMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NgConstructionReservation resources) {
        resources.setCreateTime(DateUtil.getTimeStamp());
        Integer constructionId = resources.getConstructionId();
        NgConstructionSiteInfo constructionSiteInfo = ngConstructionSiteInfoMapper.selectById(constructionId);
        if(constructionSiteInfo != null){
            resources.setTeacherId(constructionSiteInfo.getTeacherId());
        }
        save(resources);
        try {
            WebSocketServer.sendInfo(new SocketMsg("你有一条新消息[工地预约]", MsgType.INFO), "appointmentDesign");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NgConstructionReservation resources) {
        NgConstructionReservation ngConstructionReservation = getById(resources.getId());
        ngConstructionReservation.copy(resources);
        saveOrUpdate(ngConstructionReservation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<NgConstructionReservation> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NgConstructionReservation ngConstructionReservation : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", ngConstructionReservation.getName());
            map.put("电话号", ngConstructionReservation.getTelNum());
            map.put("小区名称", ngConstructionReservation.getCellName());
            map.put("面积", ngConstructionReservation.getHousingArea());
            map.put("创建者", ngConstructionReservation.getCreateBy());
            map.put("更新者", ngConstructionReservation.getUpdateBy());
            map.put("创建日期", ngConstructionReservation.getCreateTime());
            map.put("更新时间", ngConstructionReservation.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}